package com.xccwk.springmvc;


import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xccwk.model.User;
import com.xccwk.serviceImp.MybatisMapperServiceImp;

@Controller
public class Springmvc {
	MybatisMapperServiceImp imp=new MybatisMapperServiceImp();
	@RequestMapping("/login")
	public String mybatisServlet(HttpServletRequest  req,HttpServletResponse response) {
		MybatisMapperServiceImp imp=new MybatisMapperServiceImp();
		System.out.println("hello");
		List<User> listUser;
		if(req.getParameter("id")!=null) {
			int id= Integer.parseInt(req.getParameter("id"));
			listUser=imp.findUserById(id);
			System.out.println(listUser.toString());
			req.getSession().setAttribute("listUser",listUser);
			req.getSession().setAttribute("flag", false);
		}else {
			listUser=imp.findAll();
			System.out.println(listUser.toString());
			req.getSession().setAttribute("listUser",listUser);
		}	
		return "login";
	}



	@RequestMapping("/Vuage")
	public String mybatisSelectVuageServlet(HttpServletRequest  req,HttpServletResponse response) throws IOException {
		List<User> listUser;
		String like= req.getParameter("like");
		System.out.println("like"+like);
		listUser=imp.avageSelect(like);
		req.getSession().setAttribute("listUser",listUser);
		return "login";
	}



	@RequestMapping("/Save")
	public String mybatisSaveServlet(HttpServletRequest  request,HttpServletResponse response) throws IOException {
		User user=new User();
		Integer like= Integer.parseInt(request.getParameter("id"));
		request.getSession().setAttribute("flag",true);
		Integer listId=imp.selectId(like);
		if(listId!=0) {
			request.getSession().setAttribute("flag",true);
			System.out.println("id存在");
			return "redirect:/login";
		}
		user.setId(Long.parseLong(request.getParameter("id")));
		System.out.println(request.getParameter("id"));
		user.setUserName(request.getParameter("username"));
		user.setUserPassword(request.getParameter("password"));
		imp.insert(user);
		boolean flag=(Boolean) request.getSession().getAttribute("flag");
		request.getSession().setAttribute("flag", false);
		return "redirect:/login";
	}

	@RequestMapping("/Delete")
	public String mybatisDeleteServlet(HttpServletRequest  req,HttpServletResponse response) throws IOException {
		String id=req.getParameter("id");
		System.out.println("/mybatdelete"+id);
		imp.delete(Long.parseLong(id));
		req.getSession().setAttribute("flag",false);
		return "redirect:/login";
	}


}
